//
//  SLDownloadCell.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/7.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLDownloadCell.h"

@interface SLDownloadCell ()

@property (nonatomic, weak) UILabel *fileNameLabel;
@property (nonatomic, weak) UILabel *downloadStateLabel;
@property (nonatomic, weak) UILabel *downloadProgressLabel;

@end

@implementation SLDownloadCell

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    [self.contentView removeAllSubviews];
    
    UILabel *fileNameLabel = [self labelWithFont:[UIFont systemFont13] textColor:[UIColor blackColor] text:@""];
    self.fileNameLabel = fileNameLabel;
    [self.contentView addSubview:fileNameLabel];
    
    UILabel *downloadStateLabel = [self labelWithFont:[UIFont systemFont10] textColor:[UIColor darkGrayColor] text:@""];
    self.downloadStateLabel = downloadStateLabel;
    [self.contentView addSubview:downloadStateLabel];
    
    UILabel *downloadProgressLabel = [self labelWithFont:[UIFont systemFont10] textColor:[UIColor blueColor] text:@""];
    self.downloadProgressLabel = downloadProgressLabel;
    [self.contentView addSubview:downloadProgressLabel];
}

- (void)refreshUI {
    self.fileNameLabel.frame = [self.fileModel fileNameLabelFrame];
    self.fileNameLabel.text = self.fileModel.fileName;
    
    self.downloadStateLabel.frame = [self.fileModel downloadStateLabelFrame];
    self.downloadStateLabel.text = [self.fileModel getDownloadStateDesc];
    
    self.downloadProgressLabel.frame = [self.fileModel downloadProgressLabelFrame];
    self.downloadProgressLabel.text = [NSString stringWithFormat:@"%@", [self.fileModel.progress isKindOfClass:[NSNumber class]] ? self.fileModel.progress : @(0)];
}

#pragma mark - setter & getter
- (void)setFileModel:(SLDownloadFileModel *)fileModel {
    _fileModel = fileModel;
    
    [self refreshUI];
}

#pragma mark - quick
- (UILabel *)labelWithFont:(UIFont *)font textColor:(UIColor *)textColor text:(NSString *)text {
    UILabel *label = [[UILabel alloc] init];
    label.font = font;
    label.textColor = textColor;
    label.text = text;
    
    return label;
}

@end
